<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我的菜单管理页面</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/js/jquery.js"></script>
</head>
<body>
<div>
        <tbody id="menuTable">
            <table id="demo" lay-filter="demoFilter"></table>
            <div id="editModal" style="display:none;">
                <form class="layui-form" id="editForm">
                    <div class="layui-form-item">
                        <label class="layui-form-label">菜单ID</label>
                        <div class="layui-input-block">
                            <input type="text" name="murl" id="mid" readonly class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">菜单名称</label>
                        <div class="layui-input-block">
                            <input type="text" name="mname" id="mname" required placeholder="请输入菜单名称" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">菜单URL</label>
                        <div class="layui-input-block">
                            <input type="text" name="murl" id="murl" required placeholder="请输入菜单URL" class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-input-block">
                            <button class="layui-btn" lay-submit lay-filter="editSubmit">修改</button>
                            <button type="button" class="layui-btn layui-btn-cancel" id="cancelEdit">取消</button>
                        </div>
                    </div>
                </form>
            </div>
        </tbody>
    </table>
</div>
</body>
<script src="/layui/layui.js"></script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">修改</a>
</script>
<script>
    layui.use(['table', 'layer', 'form'], function () {
        var table = layui.table;
        var layer = layui.layer;
        var form = layui.form;
        //执行第一个laydate实例
        //第一个表格
        table.render({
            elem: '#demo'
            , height: 600 //设置高度
            , url: '/menu/listAll' //数据接口
            , page: true //开启分页
            , limit: 5
            , limits: [5, 10, 15, 20, 25, 30]
            , cols: [
                [ //表头
                    {field: 'mid', title: '菜单ID', width: 100},
                    {field: 'mname', title: '菜单名称', width: 300}
                    , {field: 'pid', title: '菜单权限id', width: 250}
                    , {field: 'murl', title: '菜单url地址', width: 350}
                    , {field: 'fid', title: '上级菜单', width: 200}
                    ,{title: '操作', width: 150, toolbar: '#barDemo'}
                ]
            ],
            id: 'demoReload'
        });
        table.on('tool(demoFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                // 打开弹出框
                layer.open({
                    type: 1,
                    title: '修改菜单信息',
                    area: '500px',
                    content: $('#editModal'), // 使用定义的弹出框内容
                    success: function () {
                        // 填充数据到表单
                        $('#mname').val(data.mname);
                        $('#murl').val(data.murl);
                        //给菜单ID
                        $('#mid').val(data.mid);
                        // 处理取消按钮
                        $('#cancelEdit').click(function () {
                            layer.closeAll(); // 关闭弹出框
                        });
                    }
                });
            }
        });


        // 监听表单提交
        form.on('submit(editSubmit)', function (data) {
            console.log('修改数据:', data.field);
            //发送ajax请求

            return false;
        });
    })
</script>
</html>